package com.gxar.quick.ar.server.base.controller;

import com.gxar.common.entity.Result;
import com.gxar.common.entity.SlippageResult;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.common.warning.ApiLog;
import com.gxar.quick.ar.server.base.model.ResultCodeEnum;
import com.gxar.quick.ar.server.base.model.works.vo.request.BatchDeleteDraftRequest;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksDraftCreateRequest;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksDraftUpdateRequest;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksDraftCreateResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksDraftInfoResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksDraftListResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksDraftUpdateResponse;
import com.gxar.quick.ar.server.db.service.WorkDraftService;
import com.gxar.quick.ar.server.db.service.WorksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 作品接口
 *
 * @author linsy
 * @version 1.0
 * @created 2023/11/3 15:44
 */
@Api(tags = "作品草稿接口")
@RestController
@RequestMapping("/api/works/drafts")
@RequiredArgsConstructor
@Slf4j
public class WorksDraftController {

    private final WorksService worksService;
    private final WorkDraftService workDraftService;

    @ApiOperation("创建草稿")
    @PostMapping("/create")
    @ApiLog
    public Result<WorksDraftCreateResponse> createDraft(@RequestBody @Valid WorksDraftCreateRequest worksDraftCreateRequest) {
        UserProfile userProfile = LoginUserThread.get();
        WorksDraftCreateResponse worksDraftCreateResponse = worksService.createDraft(userProfile, worksDraftCreateRequest);
        return Result.success(worksDraftCreateResponse);
    }

    @ApiOperation("查看草稿详情")
    @ApiImplicitParam(name = "id", value = "作品草稿id", dataType = "long", required = true, paramType = "path")
    @GetMapping("/{id}")
    public Result<WorksDraftInfoResponse> getDraftDetail(@PathVariable(name = "id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        return workDraftService.getDraftDetail(userProfile, id);
    }

    @ApiOperation("作品草稿列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "next_id", value = "下一个查询的id", dataType = "string"),
    })
    @GetMapping("")
    public Result<SlippageResult<WorksDraftListResponse>> getList(@RequestParam(name = "next_id", required = false) String nextId) {
        UserProfile userProfile = LoginUserThread.get();
        SlippageResult<WorksDraftListResponse> worksDraftListResponse = workDraftService.draftList(userProfile, nextId);
        return Result.success(worksDraftListResponse);
    }

    @ApiOperation("更新草稿")
    @ApiImplicitParam(name = "id", value = "作品草稿id", dataType = "long", required = true, paramType = "path")
    @PutMapping("/{id}")
    public Result<WorksDraftUpdateResponse> updateDraft(@PathVariable(name = "id") Long id, @RequestBody WorksDraftUpdateRequest worksDraftUpdateRequest) {
        UserProfile userProfile = LoginUserThread.get();
        return workDraftService.updateDraft(userProfile, id, worksDraftUpdateRequest);
    }

    @ApiOperation("草稿删除")
    @ApiImplicitParam(name = "id", value = "作品草稿id", dataType = "long", required = true, paramType = "path")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteDraft(@PathVariable(name = "id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        return workDraftService.deleteDraft(userProfile, id);
    }

    @ApiOperation("批量草稿作品删除")
    @DeleteMapping("")
    public Result<Boolean> batchDeleteDraftWorks(@RequestBody BatchDeleteDraftRequest request) {
        List<Long> ids = request.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            throw new APIRuntimeException(ResultCodeEnum.WORK_IDS_NOT_NULL);
        }
        UserProfile userProfile = LoginUserThread.get();
        return workDraftService.batchDeleteWorks(userProfile, ids);
    }
}
